package threadService

import (
	"errors"
	"log"

	"notabug.org/ropemann/imabon/src/entities/thread"
)

var GetMostRecentThreadsFunc = thread.GetMostRecentThreads
var GetAllThreadsByBoardFunc = thread.GetAllThreadsByBoard
var GetThreadByIdFunc = thread.GetThreadById
var CreateThreadFunc = thread.CreateThread

func GetMostRecentThreads() map[string]thread.Thread {
	recentThreads := GetMostRecentThreadsFunc()
	return recentThreads
}

func GetAllThreadsByBoard(board string) []thread.Thread {
	threads := GetAllThreadsByBoardFunc(board)
	return threads
}

func GetThreadById(board, id string) thread.Thread {
	thread := GetThreadByIdFunc(board, id)
	return thread
}

func PersistThread(board string, newThread thread.Thread) (string, error) {
	if len(newThread.Title) == 0 {
		return "", errors.New("title is empty")
	}
	if len(newThread.Body) == 0 {
		return "", errors.New("body is empty")
	}
	if len(newThread.Image) == 0 {
		return "", errors.New("no image was provided")
	}

	threadId, err := CreateThreadFunc(board, newThread)
	if err != nil {
		log.Printf("Error occured on thread creation: %s\n", err.Error())
		return "", err
	}
	return threadId, nil
}
